package cn.crc.sysinfo;

import java.io.*;

/**
 * 文件工具类_23.9.8
 */
public class FileUtil {
    public static void main(String[] args) throws IOException {
        String file= "D://test.txt";
        String txt="杭州亚运会开幕了";
        FileUtil.writeFile(file, txt);
        System.out.println(FileUtil.readFile(file));
    }

    /**
     * 读取文件内容
     */
    public static String readFile(String filePath) throws IOException {
        StringBuilder content = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line).append(System.lineSeparator());
            }
        }
        return content.toString();
    }

    /**
     * 写入文件内容
     */
    public static void writeFile(String filePath, String content) throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            writer.write(content);
        }
    }

}
